package com.example.androidgeneric;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.AuthFailureError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class f_write2_nearbiz extends ListFragment implements Listener<String>,
		ErrorListener, OnItemClickListener {

	private StringRequest request;
	private mAdapter adapter;
	private ArrayList<NearBizData> list;

	public static f_write2_nearbiz newInstance(float lat, float lng) {
		f_write2_nearbiz f = new f_write2_nearbiz();

		Bundle b = new Bundle();
		b.putFloat("lat", lat);
		b.putFloat("lng", lng);

		f.setArguments(b);

		return f;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		final String lat = String.valueOf(getArguments().getFloat("lat"));
		final String lng = String.valueOf(getArguments().getFloat("lng"));

		request = new StringRequest(Method.POST,
				"http://www.sharping.co.kr/rmobile/test/find_near_biz.php", this,
				this) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {

				HashMap<String, String> param = new HashMap<String, String>();
				param.put("lat", lat);
				param.put("lng", lng);

				Log.v("lat", lat);
				Log.v("lng", lng);

				return param;
			}
		};
		MyApplication.getInstance().getRequestQueue().add(request);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		setLayout();
	}

	private void setLayout() {
		getListView().setOnItemClickListener(this);
	}
	
	private class NearBizData {
		public String CUserId, CBizName, CBizCategory, CBizLat, CBizLng, distance;
	}

	@Override
	public void onErrorResponse(VolleyError err) {

	}

	@Override
	public void onResponse(String result) {

		Log.e("RESULT", result);

		try {
			Gson gson = new Gson();
			
			Type type = new TypeToken<ArrayList<NearBizData>>() {
			}.getType();
			list = gson.fromJson(result, type);
			
			adapter = new mAdapter(getActivity(), list);
			setListAdapter(adapter);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private class mAdapter extends ArrayAdapter<NearBizData> {

		LayoutInflater inflater;

		public mAdapter(Context context, List<NearBizData> objects) {
			super(context, 0, objects);

			this.inflater = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder v;

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.i_bizinfo, null);

				v = new ViewHolder();

				v.text_bizname = (TextView) convertView
						.findViewById(R.id.i_bizinfo_bizname);

				convertView.setTag(v);
			} else {
				v = (ViewHolder) convertView.getTag();
			}

			v.text_bizname.setText(getItem(position).CBizName);

			return convertView;
		}

		private class ViewHolder {
			TextView text_bizname;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View v, int position, long id) {
		
		String bizId = list.get(position).CUserId;
		String bizName = list.get(position).CBizName;
		String bizCategory = list.get(position).CBizCategory;
		
		Intent intent = new Intent();
		intent.putExtra(WritePage2.RESULT_KEY_BIZ_ID, bizId);
		intent.putExtra(WritePage2.RESULT_KEY_BIZ_NAME, bizName);
		intent.putExtra(WritePage2.RESULT_KEY_BIZ_CATEGORY, bizCategory);
		
		getActivity().setResult(Activity.RESULT_OK, intent);
		getActivity().finish();
	}
}
